﻿Option Strict Off
Imports System.IO
Imports System.Net

Public Class AppManager

    Public Shared StartURL As String = ""

    Private Shared _CurrentBrowser As exBrowser
    Public Shared Property CurrentBrowser() As exBrowser
        Get
            Return _CurrentBrowser
        End Get
        Set(ByVal value As exBrowser)
            _CurrentBrowser = value
        End Set
    End Property

    Private Shared _MainForm As BrowserWithoutTabs
    Public Shared Property MainForm() As BrowserWithoutTabs
        Get
            Return _MainForm
        End Get
        Set(ByVal value As BrowserWithoutTabs)
            _MainForm = value
        End Set
    End Property

    '//////////////////////////////////////////

    Public Shared Sub AddTab(ByVal ofrm As Form, Optional ByVal URL As String = "")
        Try
            If _MainForm.WebBrowser1.TabPages(0).CloseButtonVisible = False Then
                _MainForm.WebBrowser1.TabPages(0).CloseButtonVisible = True
            End If
        Catch ex As Exception

        End Try
        _MainForm.WebBrowser1.TabPages.Add(ofrm)
        If URL = "" Then
            _CurrentBrowser.GoHome()
        Else
            Navigate(URL)
        End If
    End Sub

    Public Shared Sub AddTab(ByVal oTab As BBrowser.TabPage, Optional ByVal URL As String = "")
        Try
            If _MainForm.WebBrowser1.TabPages(0).CloseButtonVisible = False Then
                _MainForm.WebBrowser1.TabPages(0).CloseButtonVisible = True
            End If
        Catch ex As Exception

        End Try
        _MainForm.WebBrowser1.TabPages.Add(oTab.Form)
        If URL = "" Then
            _CurrentBrowser.GoHome()
        Else
            Navigate(URL)
        End If
    End Sub

    Public Shared Sub CurrentURL()
        Try
            _MainForm.txtUrl.Text = _CurrentBrowser.Url.AbsoluteUri.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Public Shared Sub CurrentURLSettings()
        Try
            My.Settings.CurrentUrlForTabs = _CurrentBrowser.Url.AbsoluteUri.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Public Shared Sub CurrentTitleSettings()
        Try
            My.Settings.CurrentDocumentTitleForTabs = _CurrentBrowser.DocumentTitle.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Public Shared Sub Navigate(ByVal URL As String)
        _CurrentBrowser.Navigate(URL)
    End Sub

    Public Shared Function FixURL(ByVal sURL As String) As String
        If Not sURL.ToLower().StartsWith("http://") _
        Then sURL = "http://" & sURL
        Return sURL
    End Function

    Public Shared Function GetWebImage(ByVal ImageURL As String) As Image
        Dim objImage As MemoryStream
        Dim objwebClient As WebClient
        Dim sURL As String = Trim(ImageURL)
        Dim oImage As Image
        Try
            If Not sURL.ToLower().StartsWith("http://") _
            Then sURL = "http://" & sURL
            objwebClient = New WebClient
            objImage = New  _
            MemoryStream(objwebClient.DownloadData(sURL))
            oImage = System.Drawing.Image.FromStream(objImage)
            Return oImage
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Shared Function GetFavIcon(ByVal IconURL As String) As Icon
        Dim oIcon As Icon
        Dim objIcon As MemoryStream
        Dim objwebClient As WebClient
        Dim sURL As String = Trim(IconURL)

        Try
            If Not sURL.ToLower().StartsWith("http://") _
            Then sURL = "http://" & sURL
            objwebClient = New WebClient
            objIcon = New MemoryStream(objwebClient.DownloadData(sURL))
            oIcon = New Icon(objIcon)
            If IsNothing(oIcon) Then
                Return _MainForm.Icon
            Else
                Return oIcon
            End If
            objIcon.Dispose()
            objwebClient.Dispose()
            objwebClient = Nothing
            objIcon = Nothing
        Catch ex As Exception
            Return _MainForm.Icon
        End Try
    End Function

    Public Shared Function LoadWebImageToPictureBox(ByVal pb As PictureBox, ByVal ImageURL As String) As Boolean
        Dim objImage As MemoryStream
        Dim objwebClient As WebClient
        Dim sURL As String = Trim(ImageURL)
        Dim bAns As Boolean

        Try
            If Not sURL.ToLower().StartsWith("http://") _
            Then sURL = "http://" & sURL
            objwebClient = New WebClient
            objImage = New  _
            MemoryStream(objwebClient.DownloadData(sURL))
            pb.Image = System.Drawing.Image.FromStream(objImage)
            bAns = True
        Catch ex As Exception
            bAns = False
        End Try

        Return bAns
    End Function

    Public Shared Function IsValidUrl(ByVal url As String) As Boolean
        Return System.Text.RegularExpressions.Regex.IsMatch(url, _
        "(http|ftp|https)://([\w-]+\.)+(/[w- ./?%&=]*)?")
    End Function

    Public Shared Function IsValidIP(ByVal ipAddress As String) As Boolean
        Return System.Text.RegularExpressions.Regex.IsMatch(ipAddress, _
        "^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$")
    End Function

    Public Shared Function IsValidEmail(ByVal email As String) As Boolean
        Return System.Text.RegularExpressions.Regex.IsMatch(email, _
        "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
    End Function

    Public Shared Sub ShellandWait(ByVal ProcessPath As String)
        Dim objProcess As System.Diagnostics.Process
        Try
            objProcess = New System.Diagnostics.Process()
            objProcess.StartInfo.FileName = ProcessPath
            objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
            objProcess.Start()

            objProcess.WaitForExit()

            objProcess.Close()
        Catch ex As Exception
            MessageBox.Show("Could not start process " & ProcessPath, "Error!")
        End Try
    End Sub
End Class
